#!/bin/bash -E

# this should take latest varsion of peac-installer and execute it

PEAC_VERSION=0.5

PEAC_CONF_FILE="peac.cf"
PEAC_BUILD_CONF_FILE="sw/peac-build.cf"

function AddVariableToConfig() {
  MYTMP=`cat $1 | grep $2`
  if [ -z "$MYTMP" ];then
    echo "" >> $1
    if [ "$4" == "c" ];then
      echo "# export $2=\"$3\"" >> $1
    elif [ "$4" == "e" ];then
      echo "$2=\"$3\"" >> $1
    else
      echo "export $2=\"$3\"" >> $1
    fi
  fi
}

function CreateDir() {
  if [ ! -d $1 ];then
    mkdir -p $1
  fi
}
function CheckPEACConfigFile() {
  if [ ! -f $PEAC_CONF_FILE ]; then
    echo "Creating default `pwd`/$PEAC_CONF_FILE file ..."
    echo "# generated `date`" > $PEAC_CONF_FILE
  fi

  AddVariableToConfig $PEAC_CONF_FILE PEAC_SYS_DIR `pwd`

  AddVariableToConfig $PEAC_CONF_FILE PEAC_USER $USER
  AddVariableToConfig $PEAC_CONF_FILE PEAC_GROUP `id -g -n`
  AddVariableToConfig $PEAC_CONF_FILE PEAC_LOG_DIR `pwd`/logs
  AddVariableToConfig $PEAC_CONF_FILE PEAC_SW_XROOTD_PORT "1094"
  AddVariableToConfig $PEAC_CONF_FILE PEAC_SW_XRD_OPTS ""
  AddVariableToConfig $PEAC_CONF_FILE PEAC_SW_XRD_OPTS "-DIConnectTimeout 10 -DITransactionTimeout 10 -DIRequestTimeout 10 -DIRedirCntTimeout 10" "c"
  AddVariableToConfig $PEAC_CONF_FILE PEAC_POD_MASTER_PORT "1093"
  AddVariableToConfig $PEAC_CONF_FILE PEAC_POD_WK_CF "`pwd`/pod_ssh.cf"
  AddVariableToConfig $PEAC_CONF_FILE PEAC_POD_SYS_DIR "`pwd`/PoD"

  AddVariableToConfig $PEAC_CONF_FILE PEAC_POD_VERSION_CURRENT "VO_PEAC@PoD::3.5.59.g6faa-Source"
  AddVariableToConfig $PEAC_CONF_FILE PEAC_XROOT_VERSION_CURRENT "VO_PEAC@Xrootd::3.0.3"
#   AddVariableToConfig $PEAC_CONF_FILE PEAC_ROOT_VERSION_CURRENT "VO_PEAC@ROOT::v5-28-00d"

  AddVariableToConfig $PEAC_CONF_FILE MLSENSOR_TARGETML "`hostname -f`" "c"
  AddVariableToConfig $PEAC_CONF_FILE MLSENSOR_CLUSTERNAME "PEAC::CERN::FIXME" "c"

  AddVariableToConfig $PEAC_CONF_FILE PEAC_SCRIPT_DIR `pwd`/scripts/peac
  AddVariableToConfig $PEAC_CONF_FILE PEAC_SW_DIR `pwd`/sw
  AddVariableToConfig $PEAC_CONF_FILE PEAC_SW_TEMPLATE_DIR `pwd`/sw/template
  AddVariableToConfig $PEAC_CONF_FILE PEAC_SW_TEMPLATE_USER_DIR `pwd`/sw/template/user
  AddVariableToConfig $PEAC_CONF_FILE PEAC_SW_TMP_DIR `pwd`/sw/tmp
  AddVariableToConfig $PEAC_CONF_FILE PEAC_SW_SRC_DIR `pwd`/sw/src
  AddVariableToConfig $PEAC_CONF_FILE PEAC_SW_PACKAGE_DIR `pwd`/sw/binary
  AddVariableToConfig $PEAC_CONF_FILE PEAC_SW_PACKAGE_XRD_DIR `pwd`/sw/binary
  AddVariableToConfig $PEAC_CONF_FILE PEAC_SW_PACKAGE_DEP_DIR `pwd`/sw/dep
  AddVariableToConfig $PEAC_CONF_FILE PEAC_SW_XRD_DIR `pwd`/xrd

  source $PEAC_CONF_FILE
  
  CreateDir $PEAC_LOG_DIR
  CreateDir $PEAC_SCRIPT_DIR
  CreateDir $PEAC_SW_DIR
  CreateDir $PEAC_SW_TEMPLATE_DIR
  CreateDir $PEAC_SW_TEMPLATE_DIR/peac
  CreateDir $PEAC_SW_TEMPLATE_USER_DIR
  CreateDir $PEAC_SW_TMP_DIR
  CreateDir $PEAC_SW_SRC_DIR
  CreateDir $PEAC_SW_PACKAGE_DIR
  CreateDir $PEAC_SW_PACKAGE_DEP_DIR

  if [ ! -f $PEAC_BUILD_CONF_FILE ]; then
    echo "Creating default `pwd`/$PEAC_BUILD_CONF_FILE file ..."
    echo "" > $PEAC_BUILD_CONF_FILE
  fi

  AddVariableToConfig $PEAC_BUILD_CONF_FILE  PEAC_BUILD_MAKEOPTS "-j`cat /proc/cpuinfo | grep processor | wc -l`" "e"
  AddVariableToConfig $PEAC_BUILD_CONF_FILE  PEAC_BUILD_LOG "0" "e"
  AddVariableToConfig $PEAC_BUILD_CONF_FILE  PEAC_NICE_CMD "nice -n 15" "e"

  source $PEAC_BUILD_CONF_FILE

}

function UpdatePEACFile() {
  	echo "Upgrading $1 ..."
	rm -f $2/$1 
	wget -q -O $2/$1 $3/$1
	if [ "$?" != "0" ];then 
	  echo "Problem downloading latest $1 !!!"
	  rm -f $2/$1
	  exit 1
	fi
	chmod +x $2/$1
}

function PrintPEACFileInfo() {
    local MY_VER=`cat $2/$1 | grep PEAC_VERSION | head -n 1 | awk -F '=' '{print $2}'`
    echo "$1 v$MY_VER"
}

function CheckPEACFile() {

  if [ "$4" == "c" ];then
    PrintPEACFileInfo $1 $2
  elif [ "$4" == "u" ];then
    CheckPEACVersion $1 $2 $3
    if [ "$?" != "0" ]; then
      UpdatePEACFile $1 $2 $3
      PrintPEACFileInfo $1 $2
    elif [ "$?" != "1" ]; then
      UpdatePEACFile $1 $2 $3
      PrintPEACFileInfo $1 $2
    else 
      PrintPEACFileInfo $1 $2
    fi
  else
    if [ ! -f "$2/$1" ];then
      UpdatePEACFile $1 $2 $3
      PrintPEACFileInfo $1 $2
    fi
  fi
}

function CheckPEACVersion() {
  if [ ! -f "$2/$1" ];then
    return 1
  fi

  local MY_CURRENT_FILE="$2/$1"
  local MY_LATEST_FILE="$3/$1"

  existing_version=`cat $MY_CURRENT_FILE | grep PEAC_VERSION | head -n 1`
  existing_version=`echo $existing_version | sed -r -e 's/.*PEAC_VERSION\s*=\s*([[:digit:]]+.[[:digit:]]+).*/\1/'`
  
  latest_version=`wget -q -O - $MY_LATEST_FILE | grep PEAC_VERSION | head -n 1`
  latest_version=`echo $latest_version | sed -r -e 's/.*PEAC_VERSION\s*=\s*([[:digit:]]+.[[:digit:]]+).*/\1/'`
  
  if [ "$existing_version" != "$latest_version" ];then
    return 2
  fi

  return 0
}

function ShowHelp() {
  echo "./peac <mode>"
  echo "  modes :"
  for f in `ls $PEAC_SCRIPT_DIR/*.sh`; do
    echo "          `basename $f | awk -F 'peac-' '{print $2}' | awk -F '.sh' '{print $1}' ` "
  done
  echo ""
}

function RunPEACScript() {
  NAME=$1
  shift
  # check is $NAME is empty
  if [ -z $NAME ];then
    ShowHelp
    exit 0
  fi

  # check if executable exist
  if [ ! -f $PEAC_SCRIPT_DIR/peac-$NAME.sh ];then
    ShowHelp
    exit 0
  fi
  . $PEAC_SCRIPT_DIR/peac-$NAME.sh $*
}

function UpdatePeacFiles() {
   for MY_TEST in $1;do
    CheckPEACFile "$MY_TEST" "$2" "$3" "$4" "$5"
  done
}

function UpdatePeac() {
  # upgrade latest 

  MY_TEST_FILES="peac-sw.sh peac-proof.sh"
  UpdatePeacFiles "$MY_TEST_FILES" $PEAC_SCRIPT_DIR http://peac.googlecode.com/svn/trunk/scripts $1
  
  MY_TEST_FILES="peac-base.pbuild peac-distribution.pbuild peac-generate_env_peac.pbuild peac-generate_env.pbuild peac-install.pbuild peac-make.pbuild  peac-make_clean.pbuild peac-test.pbuild"
  MY_TEST_FILES="$MY_TEST_FILES peac-configure.pbuild peac-download.pbuild peac-identify_system.pbuild peac-make_install.pbuild peac-run.pbuild peac-unpack.pbuild peac-check_binary.pbuild peac-lock.pbuild"
  MY_TEST_FILES="$MY_TEST_FILES peac-svn_info.pbuild peac-check_local_binary.pbuild peac-send_to_master.pbuild"
  UpdatePeacFiles "$MY_TEST_FILES" $PEAC_SW_TEMPLATE_DIR/peac http://peac.googlecode.com/svn/trunk/sw/template/peac $1
  
  MY_TEST_FILES="VO_ALICE@AliRoot.pbuild VO_ALICE@Geant3.pbuild VO_ALICE@OpenSSL.pbuild VO_ALICE@ROOT.pbuild VO_ALICE@Xrootd.pbuild VO_ALICE@Xalienfs.pbuild"
  MY_TEST_FILES="$MY_TEST_FILES VO_PEAC@Cmake.pbuild VO_PEAC@ROOT.pbuild VO_PEAC@Xrootd.pbuild VO_PEAC@PoD.pbuild"
  UpdatePeacFiles "$MY_TEST_FILES" $PEAC_SW_TEMPLATE_DIR http://peac.googlecode.com/svn/trunk/sw/template $1

  MY_TEST_FILES="VO_PEAC.dep VO_PEAC.soft VO_PEAC_WK.soft VO_ALICE.dep VO_ALICE.soft"
  UpdatePeacFiles "$MY_TEST_FILES" $PEAC_SW_PACKAGE_DEP_DIR http://peac.googlecode.com/svn/trunk/sw/dep $1

}

function UpdatePeacScript() {
  echo ""
  echo "PEAC update available !!!!"
  echo "  To upgrade to lates peac version do following : "
  echo ""
  echo "  rm peac"
  echo "  wget http://peac.googlecode.com/svn/trunk/scripts/peac"
  echo "  chmod +x peac"
  echo ""
  exit 1
}

CheckPEACVersion peac `pwd` http://peac.googlecode.com/svn/trunk/scripts
if [ "$?" != "0" ];then
  UpdatePeacScript
fi

CheckPEACConfigFile

UpdatePeac

if [ -z "$PEAC_MASTER_HOSTNAME" ];then
  echo 
  echo "**** If this machine is master please run following command *******"
  echo ""
  echo "  echo \"export PEAC_MASTER_HOSTNAME=`hostname -f`\" >> $PEAC_SYS_DIR/peac.cf"
  echo ""
  echo "*******************************************************************"
fi

if [ "$#" == "0" ]; then
  ShowHelp
  exit 1
fi

case $1 in
  help)
  ShowHelp
  ;;
  update)
  UpdatePeac u
  ;;
  check)
  UpdatePeac c
  ;;

  *)
  NAME="$1"
  shift
  RunPEACScript $NAME $*
  exit 0
  ;;
esac

# onexit